package applet.resource;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;

/** A single drawable image. */
public class Sprite0D {
	/** */
	private final String filename;
	/** */
	private BufferedImage image;

	/**
	 * Create and load a new sprite with the given filename.
	 * 
	 * @param newFilename
	 *            the name of the file to load.
	 */
	public Sprite0D(final String newFilename) {
		filename = newFilename;

		image = null;
		try {
			URL url = Sprite2D.class.getResource("/sprites/" + newFilename);
			image = ImageIO.read(url);
		} catch (Exception e) {
			System.err.println("ERROR: Image file \"/sprites/" + newFilename
					+ "\" could not be found.");
		}
	}

	/** @return the name of the file. */
	public final String getFilename() {
		return filename;
	}

	/** @return the image. */
	public final BufferedImage getImage() {
		return image;
	}

	/**
	 * Draws the sprite to the given graphics target.
	 * 
	 * @param g
	 *            the graphics target.
	 * @param x
	 *            the horizontal position in pixels.
	 * @param y
	 *            the vertical position in pixels.
	 */
	public final void draw(final Graphics g, final double x, final double y) {
		final double half = 0.5;
		g.drawImage(image, (int) (x + half), (int) (y + half), null);
	}

	/**
	 * Draws the sprite to the given graphics target.
	 * 
	 * @param g
	 *            the graphics target.
	 * @param x
	 *            the horizontal position in pixels.
	 * @param y
	 *            the vertical position in pixels.
	 */
	public final void draw(final Graphics g, final int x, final int y) {
		g.drawImage(image, x, y, null);
	}
}
